home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / fixed_array.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  1.7 KB  |  95 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class FIXED_ARRAY[E]
  5.    -- 
  6.    -- The `lower' bound is frozen. Thus, FIXED_ARRAY should
  7.    -- run a little bit faster than ARRAY.
  8.    --
  9.  
  10. inherit COLLECTION[E];
  11.  
  12. creation {ANY}
  13.    make, resize
  14.  
  15. feature
  16.  
  17.    lower: INTEGER is 0;
  18.      -- Lower index bound.
  19.    
  20.    upper: INTEGER;
  21.      -- Upper index bound.
  22.    
  23. feature {NONE}
  24.    
  25.    storage: POINTER;
  26.      -- Internal access to storage location.
  27.      -- Corresoponding C type is computed according 
  28.      -- to generic type E.
  29.    
  30. feature -- Creation and Modification :
  31.    
  32.    make(size: INTEGER) is
  33.       require
  34.      size >= 0
  35.       do
  36.      if storage /= Void then
  37.         c_inline_c("free(C->_storage);");
  38.      end;
  39.      c_inline_c("C->_storage=malloc(((size_t)%
  40.             %sizeof(*(C->_storage))*a1));");
  41.      upper := size - 1;
  42.      clear_all;
  43.       ensure
  44.      count = size;
  45.      all_cleared
  46.       end;
  47.    
  48. feature -- Accessing :
  49.  
  50.    infix "@", item(index: INTEGER): E is
  51.       do
  52.      c_inline_c("R=(C->_storage)[a1];")
  53.       end;
  54.    
  55. feature -- Modification :
  56.    
  57.    put(element: E; index: INTEGER) is
  58.       do
  59.      c_inline_c("(C->_storage)[a2]=a1;");
  60.       end;
  61.  
  62.    clear is
  63.      -- Empty the array, discard all items.
  64.       do
  65.      upper := -1;
  66.      if storage /= Void then
  67.         c_inline_c("free(C->_storage);");
  68.         storage := Void;
  69.      end;
  70.       end;
  71.  
  72.    copy(other: like Current) is
  73.      -- Copy `other' onto Current.
  74.       local
  75.      i: INTEGER;
  76.       do
  77.      if upper /= other.upper then
  78.         make(other.upper);
  79.      end;
  80.      from
  81.         i := upper;
  82.      until
  83.         i = lower
  84.      loop
  85.         put(other.item(i),i);
  86.         i := i - 1;
  87.      end;
  88.       end;
  89.    
  90. invariant
  91.    
  92.    upper >= lower implies storage /= Void;
  93.       
  94. end -- FIXED_ARRAY[E]
  95.